var express = require('express');
const db = require("../db/db");
const common = require("../lib/common");
var router = express.Router();

router.post('/login', function(req, res, next) {
    let {name,pwd,identifyCode,code} = req.body;
    if (identifyCode !== code.toString()){
        res.send({flag:false,msg:"验证码错误"})
    }else {
        let sql = `select * from admin where name = '${name}'`;
        db(sql,function (err,result) {
            if (err){
                res.send({flag:false,msg:"登录失败"});
                throw err;
            }else {
                if(result[0] == null){
                    res.send({flag:false,msg:"该账号不存在"});
                }else if(result[0].pwd !== common.md5(pwd)){
                    res.send({flag:false,msg:"密码不正确"});
                }else{
                    req.session.name = name
                    res.json({flag:true,msg:"登录成功！"})
                }
            }
        })
    }
});

module.exports = router;